//
//  ST_PlayVideoProgressView.swift
//  BTVideo
//
//  Created by DouLei on 2019/9/18.
//  Copyright © 2019 DouLei. All rights reserved.
//

import UIKit
import SwifterSwift

class ST_PlayVideoProgressView: UIView {
    
    static let height = 2.cgFloat
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    MARK: - Properties
    private let leftEdge = 5.cgFloat
    
    private let spacing = 2.cgFloat
    
    private var itemWidth = 80.cgFloat
    
    private  var lineArray = [ST_PlayVideoProgressItemView]()
//    MARK: - public Method
    func create_items(count:Int) {
        
        lineArray.removeAll()
        self.removeSubviews()
        
//        itemWidth = (ScreenWidth - 2*leftEdge-(count.cgFloat-1)*spacing)/count.cgFloat
        
        for idx in 0 ..< count{
            
//            let x = leftEdge + idx.cgFloat * itemWidth + (idx.cgFloat-1)*spacing
            
            let item = ST_PlayVideoProgressItemView()
            
            self.addSubview(item)
            
            item.snp.makeConstraints { (make) in
                if idx == 0{
                    make.left.equalToSuperview().offset(leftEdge)
                }else{
                    make.left.equalTo(self.subviews[idx-1].snp.right).offset(spacing)
                }
                make.height.equalTo(ST_PlayVideoProgressView.height)
                make.centerY.equalToSuperview()
                
                if count < 2{
                    make.right.equalToSuperview().offset(-leftEdge)
                }else{
                    if idx > 0{
                        make.width.equalTo(self.subviews[0])
                    }
                    if idx == count - 1{
                        make.right.equalToSuperview().offset(-leftEdge)
                    }
                }
            }
            
            
            
            
            lineArray.append(item)
        }
    }
    
    func setProgress(percentValue:CGFloat,currentIdx:Int){
        
        
//        print("进度条:-percentValue:",percentValue,"---currentIdx:",currentIdx)
        
        guard let item = lineArray[safe:currentIdx] else { return  }
        
        if currentIdx > 0{
            
            for i in 0..<currentIdx{
                
                guard let beforeItem = lineArray[safe:i] else { continue  }
                
                beforeItem.playPercent(progress: 1)
                
//                print("进度条: beforeItem idx:",i)
            }
        }
        
//        不是最后一个
        if currentIdx < lineArray.count - 1{
            
            for i in (currentIdx + 1) ..< lineArray.count {
                
                guard let afterItem = lineArray[safe:i] else { continue  }
                
                afterItem.playPercent(progress: 0)
                
//                print("进度条: afterItem idx:",i)
            }
            
        }
        
        
//        print("进度条: currentItem idx:",currentIdx)
        item.playPercent(progress: percentValue)
        
    }
    
}



class ST_PlayVideoProgressItemView: UIView {
    
    fileprivate let radius = 3.cgFloat
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.cornerRadius = radius
        self.backgroundColor = Color.init(white: 1, alpha: 0.4)
        self.addSubview(progressLine)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var progressLine:UIView = {
        let control = UIView.init(frame: CGRect.init(x: 0, y: 0, width: 0, height: ST_PlayVideoProgressView.height))
        control.backgroundColor = UIColor.white
        control.cornerRadius = radius
        return control
    }()
    
//    MARK: - public method
    func playPercent(progress:CGFloat){
        
        if progress >= 1{
            progressLine.width = self.bounds.width
        }else{
            progressLine.width = progress * self.bounds.width
        }
    }
    
    
    
}
